//
//  LGAuthenticateViewController+LGCollection.m
//  LuxuryGoods
//
//  Created by 纳里健康 on 2018/6/8.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGAuthenticateViewController+LGCollection.h"


@implementation LGAuthenticateViewController (LGCollection)

- (void)makeCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置滑动方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    flowLayout.minimumLineSpacing = 5;
    
    flowLayout.minimumInteritemSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(120, 0, SCREEN_WIDTH - 120, SCREEN_HEIGHT - NAVIGATOR_HEIGHT - 40) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    self.collectionView.scrollEnabled = YES;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LGAuthenticateCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"LGAuthenticateCollectionViewCell"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.selectCount > 0 ? 2 : 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return section == 0 ? 10 : self.selectCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"LGAuthenticateCollectionViewCell";

    LGAuthenticateCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    cell.cellTag = indexPath.row;
    
    if(indexPath.section == 0)
    {
        cell.countView.hidden = YES;
    }else
    {
        cell.countView.hidden = NO;
    }
    
    cell.delegate = self;
    
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = ceilf(self.collectionView.width - 10 * 3)/3;
    
    return  CGSizeMake(itemWidth, itemWidth + FONT(12).lineHeight + 40);
}

#pragma mark  定义整个CollectionViewCell的四个边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);//（上、左、下、右）
}


#pragma mark -- cell点击事件
- (void)addProductToSelectAtIndex:(NSInteger)tag
{
    self.selectCount ++ ;
    
    [self.collectionView reloadData];
}

- (void)productCountAddAtIndex:(NSInteger)tag
{
    LGAuthenticateCollectionViewCell *cell = (LGAuthenticateCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:tag inSection:1]];
    
    NSInteger count = cell.countLabel.text.integerValue;
    
    count ++ ;
    
    cell.countLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
    
}

- (void)productCountReduceAtIndex:(NSInteger)tag
{
    LGAuthenticateCollectionViewCell *cell = (LGAuthenticateCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:tag inSection:1]];
    
    NSInteger count = cell.countLabel.text.integerValue;
    
    count -- ;
    
    if(count == 0)
    {
        self.selectCount --;
        [self.collectionView reloadData];
    }else
    {
       cell.countLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
    }

}

@end
